﻿using System.Windows.Automation;
using ProdControls.Interfaces;
using ProdControls.UIAPatterns;
using ProdLogging;

namespace ProdControls.Bridges
{
    public static class GridBridge
    {
        /// <summary>
        ///   Gets the total number of columns in a grid.
        /// </summary>
        /// <param name="extension"> The extended interface. </param>
        /// <param name="control"> The UI Automation element </param>
        /// <returns> The total number of columns. </returns>
        public static int GetColumnCountHook(this IGrid extension, BaseProdControl control)
        {
            return UiaGetColumnCount(control);
        }

        private static int UiaGetColumnCount(BaseProdControl control)
        {
            int retVal = GridPatternHelper.GetColumnCount(control.UIAElement);
            LogController.ReceiveLogMessage(new LogMessage(retVal.ToString()));
            return retVal;
        }

        /// <summary>
        ///   Gets the total number of rows in a grid.
        /// </summary>
        /// <param name="extension"> The extended interface. </param>
        /// <param name="control"> The UI Automation element </param>
        /// <returns> The total number of rows. </returns>
        public static int GetRowCountHook(this IGrid extension, BaseProdControl control)
        {
            return UiaGetRowCount(control);
        }

        private static int UiaGetRowCount(BaseProdControl control)
        {
            int retVal = GridPatternHelper.GetRowCount(control.UIAElement);
            LogController.ReceiveLogMessage(new LogMessage(retVal.ToString()));
            return retVal;
        }

        /// <summary>
        ///   Retrieves an AutomationElement that represents the specified cell.
        /// </summary>
        /// <param name="extension"> The extended interface. </param>
        /// <param name="control"> The UI Automation element </param>
        /// <param name="row"> The ordinal number of the row of interest. </param>
        /// <param name="column"> The ordinal number of the column of interest. </param>
        /// <returns> An AutomationElement that represents the retrieved cell </returns>
        public static AutomationElement GetItemHook(this IGrid extension, BaseProdControl control, int row, int column)
        {
            return UiaGetItem(control, row, column);
        }

        private static AutomationElement UiaGetItem(BaseProdControl control, int row, int column)
        {
            AutomationElement retVal = GridPatternHelper.GetItem(control.UIAElement, row, column);
            LogController.ReceiveLogMessage(new LogMessage(retVal.Current.Name));
            return retVal;
        }
    }
}